package cn.life.business.schedule.service.impl;

import cn.life.business.material.entity.vo.MaterialRecVo;
import cn.life.business.schedule.entity.po.ScheduleAuditPo;
import cn.life.business.user.dao.UserDao;
import cn.life.common.entity.model.CMessage;
import cn.life.business.schedule.dao.ScheduleDao;
import cn.life.business.schedule.entity.po.Schedule;
import cn.life.business.schedule.entity.vo.ScheduleVo;
import cn.life.business.schedule.service.ScheduleService;
import cn.life.common.entity.model.MyUserInfo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.push.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 王振宇
 * @Description:
 * @Date 2018-07-13 17:18:00
 */
@Service
@Transactional
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleDao scheduleDao;

    @Autowired
    private UserDao userDao;

    @Override
    public CMessage addschedule(Schedule schedule) {
        Integer number = scheduleDao.queryNowMonthNumber(schedule.getProjectId());
        if(number == 0){
            //1.新增进度信息
            Long recordId = scheduleDao.addSchedule(schedule);

            //2.新增审核记录信息
            if(recordId != null){
                ScheduleAuditPo scheduleAuditPo = new ScheduleAuditPo();
                scheduleAuditPo.setRecordId(recordId);
                scheduleAuditPo.setOprUserId(schedule.getNextUserId());
                scheduleAuditPo.setOprUserName(schedule.getNextUserName());
                scheduleDao.addScheduleAuditRecord(scheduleAuditPo);
            }

            //3.消息推送
            sendMessage(schedule.getNextUserId().toString());
            return CMessage.success();
        }else {
            return CMessage.fail("当月已提交");
        }
    }

    @Override
    public CMessage deleteschedule(Schedule schedule) {
        return scheduleDao.deleteSchedule(schedule) == 1 ?CMessage.success() : CMessage.fail("删除工厂项目进度记录失败！");
    }

    @Override
    public CMessage updateschedule(Schedule schedule) {
        //1.修改主表信息
        scheduleDao.updateSchedule(schedule);
        //2.增加新的待审核信息
        ScheduleAuditPo scheduleAuditPo = new ScheduleAuditPo();
        scheduleAuditPo.setRecordId(schedule.getRecordId());
        scheduleAuditPo.setOprUserId(schedule.getNextUserId());
        scheduleAuditPo.setOprUserName(schedule.getNextUserName());
        scheduleAuditPo.setOprDate(new Date());
        scheduleDao.addScheduleAuditRecord(scheduleAuditPo);
        return CMessage.success();
    }

    @Override
    public CMessage getAllScheduleInfo(ScheduleVo scheduleVo,MyUserInfo myUserInfo) {
        if(scheduleVo.getScheduleName() == null){
            scheduleVo.setScheduleName("");
        }
        if(scheduleVo.getProjectId() == null){
            scheduleVo.setProjectId(myUserInfo.getProjectId());
        }
        Pagination<Schedule> result = new Pagination<Schedule>();
        List<Schedule> list = new ArrayList<>();
        /*
        //待我审核
        List<Schedule> auditList = scheduleDao.queryRecordList(myUserInfo.getUserId(),scheduleVo.getScheduleName(),scheduleVo.getProjectId());
        list.addAll(auditList);
        //我发起的
        List<Schedule> oprList = scheduleDao.queryOprRecordList(myUserInfo.getUserId(),scheduleVo.getScheduleName(),scheduleVo.getProjectId());
        list.addAll(oprList);*/

        list = scheduleDao.queryLastRecordList(myUserInfo.getUserId(),scheduleVo.getScheduleName(),scheduleVo.getProjectId(),scheduleVo.getAuditState(),scheduleVo.getProjectName());

        result.setPageIndex(myUserInfo.getPageIndex());
        result.setPageSize(myUserInfo.getPageSize());
        result.setTotal(list.size());

        int startNumber = scheduleVo.getPageIndex() * scheduleVo.getPageSize();
        int endNumber = (scheduleVo.getPageIndex() + 1) * scheduleVo.getPageSize() - 1;
        if(startNumber > list.size()){
            result.setRows(new ArrayList<Schedule>());
        }else if(endNumber < list.size()){
            result.setRows(list.subList(startNumber,endNumber));
        }else if (endNumber > list.size()){
            endNumber = list.size();
            result.setRows(list.subList(startNumber,endNumber));
        }

        for(Schedule schedule : list){
            if(schedule.getAuditState() == 0){
                schedule.setIsMyAudit(0);
            }
        }

        return CMessage.successByData(result);
    }

    @Override
    public CMessage getSchedule(ScheduleVo scheduleVo) {
        scheduleVo=scheduleDao.getSchedule(scheduleVo);
        return CMessage.successByData(scheduleVo);
    }

    @Override
    public CMessage getAjax(ScheduleVo scheduleVo) {
        return CMessage.successByData(scheduleDao.selectSchedule(scheduleVo));
    }

    @Override
    public CMessage auditSchedule(ScheduleVo scheduleVo) {
        if(scheduleVo.getState() == 1){
            //1.下一步
            ScheduleAuditPo scheduleAuditPo = new ScheduleAuditPo();
            scheduleAuditPo.setRecordId(scheduleVo.getRecordId());
            scheduleAuditPo.setOprUserId(scheduleVo.getNextUserId());
            scheduleAuditPo.setOprUserName(scheduleVo.getNextUserName());
            scheduleAuditPo.setOprDate(new Date());
            scheduleDao.addScheduleAuditRecord(scheduleAuditPo);
            //1.1.通知下一审核人
            CMessage sendRes = sendMessage(scheduleVo.getNextUserId().toString());
            if(sendRes.getCode().equals("1000")){
                return sendRes;
            }
        }else if(scheduleVo.getState() == 2){
            //2.通过
            //2.1.修改主表状态信息
            scheduleVo.setAuditState(9);
            scheduleDao.updateScheduleAuditResult(scheduleVo);
            //2.2.通知发送人员，进度审核通过
            CMessage sendRes = sendMessage(scheduleVo.getOprUserId().toString());
            if(sendRes.getCode().equals("1000")){
                return sendRes;
            }
        }else if(scheduleVo.getState() == 3){
            //3.未通过
            //3.1.修改主表状态信息
            scheduleVo.setAuditState(0);
            scheduleDao.updateScheduleAuditResult(scheduleVo);
            //3.2.通知发送人员，进度审核不通过
            CMessage sendRes = sendMessage(scheduleVo.getOprUserId().toString());
            if(sendRes.getCode().equals("1000")){
                return sendRes;
            }
            //3.3.所有审核记录作废
            scheduleDao.discardSchedlueAuditRec(scheduleVo.getRecordId());
            //3.4.修改主表中审核退回意见
            scheduleDao.updateScheduleReason(scheduleVo.getRecordId(),scheduleVo.getReason());
        }else{
            return CMessage.fail("请输入正确的选择");
        }
        return CMessage.successByMsg("审核完成");
    }

    @Override
    public CMessage queryProjectKanban(ScheduleVo scheduleVo, MyUserInfo myUserInfo) {
        if(scheduleVo.getScheduleName() == null){
            scheduleVo.setScheduleName("");
        }
        if(scheduleVo.getProjectId() == null){
            scheduleVo.setProjectId(myUserInfo.getProjectId());
        }
        Pagination<Schedule> result = new Pagination<Schedule>();
        List<Schedule> list = new ArrayList<>();
        /*
        //待我审核
        List<Schedule> auditList = scheduleDao.queryRecordList(myUserInfo.getUserId(),scheduleVo.getScheduleName(),scheduleVo.getProjectId());
        list.addAll(auditList);
        //我发起的
        List<Schedule> oprList = scheduleDao.queryOprRecordList(myUserInfo.getUserId(),scheduleVo.getScheduleName(),scheduleVo.getProjectId());
        list.addAll(oprList);*/

        list = scheduleDao.queryLastRecordStatisticsList(myUserInfo.getUserId(),scheduleVo.getScheduleName(),scheduleVo.getProjectId());

        result.setPageIndex(myUserInfo.getPageIndex());
        result.setPageSize(myUserInfo.getPageSize());
        result.setTotal(list.size());

        int startNumber = scheduleVo.getPageIndex() * scheduleVo.getPageSize();
        int endNumber = (scheduleVo.getPageIndex() + 1) * scheduleVo.getPageSize() - 1;
        if(startNumber > list.size()){
            result.setRows(new ArrayList<Schedule>());
        }else if(endNumber < list.size()){
            result.setRows(list.subList(startNumber,endNumber));
        }else if (endNumber > list.size()){
            endNumber = list.size();
            result.setRows(list.subList(startNumber,endNumber));
        }

        return CMessage.successByData(result);
    }


    public CMessage sendMessage(String userIds){
        List<Map<String,Object>> mapList = userDao.queryUserList(userIds);
        try {
            if(mapList != null && mapList.size() > 0){
                new PushService().allPush("进度","您有新的进度需要审核，请查收!",mapList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CMessage.fail("推送信息异常");
        }
        return CMessage.success();
    }

}
